package Task24;

import java.awt.EventQueue;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextPane;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import org.apache.log4j.Logger;
import Task24.Controller.FileAlreadyExistException;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Window {
	private JFrame frmMyCommander;
	private JTextField txtConsole;
	private JTextField txtLeft;
	private JTextField txtRight;
	private JList listLeft;
	private JList listRight;
	private JButton btnTree;
	private JPanel panelRootLeft;
	private JPanel panelRootRight;

	private static final Logger Log = Logger.getLogger(Window.class);
	private static final Logger history = Logger.getLogger("history");
	private Controller myController = new Controller();
	private String lastListFocus;
	private static String wichListTree;
	private JTextField txtPrompt;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Window window = new Window();
					window.frmMyCommander.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

		startLog();
	}

	/**
	 * Create the application.
	 */
	public Window() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {

		frmMyCommander = new JFrame();
		frmMyCommander.setTitle("My Super Commander");
		frmMyCommander.setBounds(100, 100, 671, 595);
		frmMyCommander.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JMenuBar menuBar = new JMenuBar();
		frmMyCommander.setJMenuBar(menuBar);

		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);

		JMenuItem mntmExit = new JMenuItem("Exit");
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				actionExit();
			}
		});
		mnFile.add(mntmExit);

		JMenu mnCommand = new JMenu("Command");
		menuBar.add(mnCommand);

		JMenuItem mntmCopy = new JMenuItem("Copy");
		mntmCopy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionCopy();
			}
		});
		mntmCopy.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {

			}
		});
		mnCommand.add(mntmCopy);

		JMenuItem mntmMove = new JMenuItem("Move");
		mntmMove.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionMove();
			}
		});
		mnCommand.add(mntmMove);

		JMenuItem mntmDel = new JMenuItem("Delete");
		mntmDel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionDel();
			}
		});

		JMenuItem mntmFind = new JMenuItem("Find");
		mntmFind.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionFind();
			}
		});
		mnCommand.add(mntmFind);

		JMenuItem mntmTree = new JMenuItem("Tree");
		mntmTree.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionTree();
			}
		});
		mnCommand.add(mntmTree);

		JMenuItem mntmMkdir = new JMenuItem("MkDir");
		mntmMkdir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionMkDir();
			}
		});
		mnCommand.add(mntmMkdir);
		mnCommand.add(mntmDel);

		JMenu mnHelp = new JMenu("Help");
		menuBar.add(mnHelp);

		JMenuItem mntmAbout = new JMenuItem("About");
		mntmAbout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				JOptionPane
						.showMessageDialog(
								null,
								"Super Commander Version 1.0.1"
										+ "\nCopyright © 2011-2012 by Bessonov Daniil\n"
										+ "\n6 группа. NetCracker\n",
								"Super Commander",
								JOptionPane.INFORMATION_MESSAGE);
			}
		});
		mnHelp.add(mntmAbout);

		txtConsole = new JTextField();
		txtConsole.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent arg0) {
				if (arg0.getKeyCode() == KeyEvent.VK_UP) {
					txtConsole.setText(HistoryOfCommand.getCommandUp());
				}
				if (arg0.getKeyCode() == KeyEvent.VK_DOWN) {
					txtConsole.setText(HistoryOfCommand.getCommandDown());
				}
			}
		});

		txtConsole.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String command = txtConsole.getText();
				history.info(command);
				HistoryOfCommand.resetIndex();
				ArrayList<String> aResult = tryExecuteCommandSeparateThread(command);
				txtConsole.setText("");
				displayResult(aResult, command);
			}
		});
		txtConsole.setColumns(10);

		JPanel panel = new JPanel();

		txtLeft = new JTextField();
		txtLeft.setEditable(false);
		txtLeft.setText("C:\\");
		txtLeft.setColumns(10);

		String nextRoot = "";
		File fDir = new File(""); // Define which disk is next after C:\
		File[] arrayRoots = fDir.listRoots();
		for (int i = 0; i < arrayRoots.length; i++) {
			if (arrayRoots[i].getPath().equalsIgnoreCase("C:\\")) {
				nextRoot = arrayRoots[i + 1].getPath();
				break;
			}
		}

		txtRight = new JTextField();
		txtRight.setEditable(false);
		txtRight.setText(nextRoot);
		txtRight.setColumns(10);

		JTextPane textPane = new JTextPane();

		JScrollPane scrollPane = new JScrollPane();

		JScrollPane scrollPane_1 = new JScrollPane();

		txtPrompt = new JTextField();
		txtPrompt.setEditable(false);
		txtPrompt.setText("C:\\>");
		txtPrompt.setColumns(10);

		panelRootLeft = new JPanel();

		panelRootRight = new JPanel();

		// -------------------------------------------------------------
		GroupLayout groupLayout = new GroupLayout(
				frmMyCommander.getContentPane());
		groupLayout
				.setHorizontalGroup(groupLayout
						.createParallelGroup(Alignment.TRAILING)
						.addGroup(
								groupLayout
										.createSequentialGroup()
										.addGap(11)
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																Alignment.TRAILING,
																groupLayout
																		.createSequentialGroup()
																		.addComponent(
																				txtPrompt,
																				GroupLayout.DEFAULT_SIZE,
																				188,
																				Short.MAX_VALUE)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				txtConsole,
																				GroupLayout.DEFAULT_SIZE,
																				436,
																				Short.MAX_VALUE))
														.addComponent(
																panel,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addGroup(
																Alignment.TRAILING,
																groupLayout
																		.createSequentialGroup()
																		.addGroup(
																				groupLayout
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addComponent(
																								scrollPane_1,
																								GroupLayout.DEFAULT_SIZE,
																								306,
																								Short.MAX_VALUE)
																						.addComponent(
																								txtLeft,
																								Alignment.TRAILING,
																								GroupLayout.DEFAULT_SIZE,
																								306,
																								Short.MAX_VALUE)
																						.addComponent(
																								panelRootLeft,
																								GroupLayout.DEFAULT_SIZE,
																								306,
																								Short.MAX_VALUE))
																		.addGap(18)
																		.addGroup(
																				groupLayout
																						.createParallelGroup(
																								Alignment.TRAILING)
																						.addComponent(
																								panelRootRight,
																								GroupLayout.DEFAULT_SIZE,
																								306,
																								Short.MAX_VALUE)
																						.addComponent(
																								scrollPane,
																								Alignment.LEADING,
																								GroupLayout.DEFAULT_SIZE,
																								306,
																								Short.MAX_VALUE)
																						.addComponent(
																								txtRight,
																								Alignment.LEADING,
																								GroupLayout.DEFAULT_SIZE,
																								306,
																								Short.MAX_VALUE))))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addComponent(textPane,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.addContainerGap()));
		groupLayout
				.setVerticalGroup(groupLayout
						.createParallelGroup(Alignment.TRAILING)
						.addGroup(
								groupLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.BASELINE,
																false)
														.addComponent(
																txtLeft,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																txtRight,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.TRAILING,
																false)
														.addComponent(
																panelRootRight,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																panelRootLeft,
																GroupLayout.DEFAULT_SIZE,
																30,
																Short.MAX_VALUE))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.TRAILING)
														.addComponent(
																textPane,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addGap(1)
																		.addGroup(
																				groupLayout
																						.createParallelGroup(
																								Alignment.TRAILING)
																						.addComponent(
																								scrollPane_1,
																								Alignment.LEADING,
																								GroupLayout.DEFAULT_SIZE,
																								350,
																								Short.MAX_VALUE)
																						.addComponent(
																								scrollPane,
																								Alignment.LEADING,
																								GroupLayout.DEFAULT_SIZE,
																								350,
																								Short.MAX_VALUE))
																		.addGap(18)))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																txtConsole,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																txtPrompt,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addComponent(panel,
												GroupLayout.PREFERRED_SIZE, 58,
												GroupLayout.PREFERRED_SIZE)
										.addGap(4)));

		initializeRootButtons("left");
		initializeRootButtons("right");

		listLeft = new JList();
		listLeft.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent arg0) {
				lastListFocus = "left";
				refreshTxtConsole(lastListFocus);
			}
		});
		listLeft.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// we should run command cd only if we have double click!
				if (e.getClickCount() == 2) {
					openDir("left");
				}
			}
		});

		listLeft.setBorder(new LineBorder(new Color(0, 0, 0)));
		scrollPane_1.setViewportView(listLeft);

		listRight = new JList();
		listRight.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent arg0) {
				lastListFocus = "right";
				refreshTxtConsole(lastListFocus);
			}
		});
		listRight.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				// we should run command cd only if we have double click!
				if (arg0.getClickCount() == 2) {
					openDir("right");
				}
			}
		});
		listRight.setModel(new AbstractListModel() {
			String[] values = new String[] {};

			public int getSize() {
				return values.length;
			}

			public Object getElementAt(int index) {
				return values[index];
			}
		});
		listRight.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listRight.setBorder(new LineBorder(new Color(0, 0, 0)));
		scrollPane.setViewportView(listRight);

		JButton btnNewButton_2 = new JButton("Copy");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionCopy();
			}
		});

		JButton btnNewButton_3 = new JButton("Move");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionMove();
			}
		});

		JButton btnNewButton_4 = new JButton("Find");
		btnNewButton_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				actionFind();
			}
		});

		JButton btnNewButton_5 = new JButton("MkDir");
		btnNewButton_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionMkDir();
			}
		});

		JButton btnNewButton_6 = new JButton("Delete");
		btnNewButton_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionDel();
			}
		});

		JButton btnExit = new JButton("Exit");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				actionExit();
			}
		});

		btnTree = new JButton("Tree");
		btnTree.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				actionTree();
			}
		});

		// ------------------------------------------------------------------------
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel.createSequentialGroup()
						.addComponent(btnNewButton_2, GroupLayout.DEFAULT_SIZE,
								81, Short.MAX_VALUE)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(btnNewButton_3, GroupLayout.DEFAULT_SIZE,
								81, Short.MAX_VALUE)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(btnNewButton_4, GroupLayout.DEFAULT_SIZE,
								81, Short.MAX_VALUE)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(btnTree, GroupLayout.DEFAULT_SIZE, 81,
								Short.MAX_VALUE)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(btnNewButton_5, GroupLayout.DEFAULT_SIZE,
								81, Short.MAX_VALUE)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(btnNewButton_6, GroupLayout.DEFAULT_SIZE,
								81, Short.MAX_VALUE)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(btnExit, GroupLayout.DEFAULT_SIZE, 81,
								Short.MAX_VALUE).addGap(3)));
		gl_panel.setVerticalGroup(gl_panel
				.createParallelGroup(Alignment.TRAILING)
				.addGroup(
						gl_panel.createSequentialGroup()
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(
														btnNewButton_2,
														GroupLayout.DEFAULT_SIZE,
														47, Short.MAX_VALUE)
												.addComponent(
														btnNewButton_3,
														GroupLayout.DEFAULT_SIZE,
														47, Short.MAX_VALUE)
												.addComponent(
														btnNewButton_4,
														GroupLayout.DEFAULT_SIZE,
														47, Short.MAX_VALUE)
												.addComponent(
														btnTree,
														GroupLayout.DEFAULT_SIZE,
														47, Short.MAX_VALUE)
												.addComponent(
														btnNewButton_5,
														GroupLayout.DEFAULT_SIZE,
														47, Short.MAX_VALUE)
												.addComponent(
														btnNewButton_6,
														GroupLayout.DEFAULT_SIZE,
														47, Short.MAX_VALUE)
												.addComponent(
														btnExit,
														GroupLayout.DEFAULT_SIZE,
														47, Short.MAX_VALUE))
								.addContainerGap()));
		panel.setLayout(gl_panel);
		frmMyCommander.getContentPane().setLayout(groupLayout);

		refreshListBox("right");
		refreshListBox("left");
		lastListFocus = "left";

	}

	private static void startLog() {
		Log.info("");
		Log.info("----MyCMD start...----");
	}

	private void refreshListBox(String wichList) {
		if (wichList.equalsIgnoreCase("left"))
			myController.setWorkDir(txtLeft.getText());
		if (wichList.equalsIgnoreCase("right"))
			myController.setWorkDir(txtRight.getText());

		ArrayList<String> aResultOfCommand = new ArrayList<String>();
		DefaultListModel myModel = new DefaultListModel();

		// if workDir is not root then print ".."
		String saveDir = myController.getWorkDir();
		aResultOfCommand = tryExecuteCommandSeparateThread("cd ..");
		boolean workDirIsRoot = true;
		if (aResultOfCommand != null) {
			workDirIsRoot = aResultOfCommand.get(0).startsWith("Dir is root!");
			aResultOfCommand.clear();
		}
		myController.setWorkDir(saveDir);

		aResultOfCommand = tryExecuteCommandSeparateThread("dir");
		if (!workDirIsRoot)
			myModel.addElement("..");

		if (aResultOfCommand != null) {
			for (int i = 0; i < aResultOfCommand.size(); i++) {
				myModel.addElement(aResultOfCommand.get(i));
			}
		}

		if (wichList.equalsIgnoreCase("left")) {
			listLeft.setModel(myModel);
			refreshTxtConsole("left");
		}
		if (wichList.equalsIgnoreCase("right")) {
			listRight.setModel(myModel);
			refreshTxtConsole("right");
		}
	}

	private void refreshTxtConsole(String wichListFocus) {
		if (wichListFocus.equalsIgnoreCase("left")) {
			txtPrompt.setText(txtLeft.getText() + ">");
			myController.setWorkDir(txtLeft.getText());
		}
		if (wichListFocus.equalsIgnoreCase("right")) {
			txtPrompt.setText(txtRight.getText() + ">");
			myController.setWorkDir(txtRight.getText());
		}
	}

	private void openFile(String file) {
		try {
			if (file.endsWith(".xml")) {
				openXML(file);
			} else {
				Runtime.getRuntime().exec("notepad.exe " + file);
			}
		} catch (IOException e1) {
			JOptionPane.showMessageDialog(null,
					"Error with open file\n" + e1.getMessage(),
					"Super Commander", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void openDir(String wichList) {
		Object selectValue = null;
		if (wichList.equalsIgnoreCase("left")) {
			myController.setWorkDir(txtLeft.getText());
			selectValue = listLeft.getSelectedValue();
		}
		if (wichList.equalsIgnoreCase("right")) {
			myController.setWorkDir(txtRight.getText());
			selectValue = listRight.getSelectedValue();
		}

		String command = "cd " + myController.getWorkDir() + File.separator
				+ (String) selectValue;
		ArrayList<String> result = tryExecuteCommandSeparateThread(command);

		if (result.get(0).equals("It is file."))
			openFile(myController.getWorkDir() + File.separator
					+ (String) selectValue);
		else {
			if (!myController.getHappendError()) {

				if (wichList.equalsIgnoreCase("left")) {
					txtLeft.setText(result.get(0));
					refreshListBox("left");
				}
				if (wichList.equalsIgnoreCase("right")) {
					txtRight.setText(result.get(0));
					refreshListBox("right");
				}

			}
		}
	}

	private void displayResult(ArrayList<String> aResult, String command) {
		if (command.startsWith("cd ")) {
			if (lastListFocus.equalsIgnoreCase("left")) {
				txtLeft.setText(aResult.get(0));
				refreshListBox("left");
			}
			if (lastListFocus.equalsIgnoreCase("right")) {
				txtRight.setText(aResult.get(0));
				refreshListBox("right");
			}
		}
		if (command.startsWith("copy ") || command.startsWith("move ")) {
			refreshListBox("left");
			refreshListBox("right");
		}

		if (command.startsWith("mkdir ") || command.startsWith("del ")) {
			if (lastListFocus.equalsIgnoreCase("left"))
				refreshListBox("left");

			if (lastListFocus.equalsIgnoreCase("right"))
				refreshListBox("right");
		}

		if (command.startsWith("tree")) 
			displayResultInSuperWindow(aResult, "tree ");
		
		if (command.startsWith("find ") ) 
			displayResultInSuperWindow(aResult, "find ");
		
	}

	private ArrayList<String> tryExecuteCommand(String command) { // <<<<<<<<<<<<<<<<<<<<<<<<<<<<

		ArrayList<String> sResult = new ArrayList<String>();
		try {
			sResult = myController.tryRun(command);
		} catch (FileAlreadyExistException e) {
			Log.info("File exists. Rewrite? (y/n)");

			int answer = JOptionPane.showConfirmDialog(null,
					"File exists. Rewrite?", "Super Commander",
					JOptionPane.YES_NO_OPTION);
			if (answer == 0) {
				Log.info("User's answer: Yes.");
				try {
					String arg1copy = myController.getMyModel().getArg1();
					String arg2copy = myController.getMyModel().getArg2();

					String myCommand = "del "
							+ myController.getMyModel().getArg2();
					sResult = myController.tryRun(myCommand); // del

					myCommand = "copy " + arg1copy + " " + arg2copy;
					sResult = myController.tryRun(myCommand); // copy

					if (command.startsWith("move")) { // if command==move
						myCommand = "del "
								+ myController.getMyModel().getArg1();
						sResult = myController.tryRun(myCommand); // del
						myController.setHappendError(false);
					}

				} catch (FileNotFoundException e1) {
					sResult.add("Error. File not found. Sorry.");
				} catch (NullPointerException e1) {
					sResult.add("Error. Null pointerException. Sorry.");
				} catch (FileAlreadyExistException e1) {
					sResult.add("Error. File already exist. Sorry.");
				}
			} else {
				Log.info("User's answer: No.");
				sResult.add(command + " - Operation canceled.");
			}

		} catch (FileNotFoundException ex) {
			String sFileFrom = myController.getMyModel().getArg1();
			String sFileTo = myController.getMyModel().getArg2();
			String message = ex.getMessage()
					+ " in the specified directory. Do you want to create a new directory?";
			int answer = JOptionPane.showConfirmDialog(null, message,
					"Super Commander", JOptionPane.YES_NO_OPTION);
			Log.info(message);

			if (answer == 0) {
				Log.info("User's answer: Yes.");
				File fileTo = new File(sFileTo);
				File newDir = new File(fileTo.getParent());

				if (newDir.mkdirs()) { // Created directory
					myController.getMyModel().setArg1(sFileFrom); // Try made
																	// copy
																	// method
					myController.getMyModel().setArg2(sFileTo);
					myController.getMyModel().setNumArg(1);
					myController.getMyModel().setWorkDir(
							myController.getWorkDir());
					try {

						File newFile = new File(myController.getMyModel()
								.getArg2()); // Get
						// name
						// of
						// file without
						// absolute path
						String myCommand = "copy "
								+ myController.getMyModel().getArg1() + " "
								+ newFile.getName();
						String workDirCopy = myController.getWorkDir(); // for
																		// create
						// arg2 in
						// absolute path
						myController.setWorkDir(newFile.getParent());

						sResult = myController.tryRun(myCommand); // copy
						myController.setWorkDir(workDirCopy);

						if (command.startsWith("move")) { // if command==move
							myCommand = "del "
									+ myController.getMyModel().getArg1();
							sResult = myController.tryRun(myCommand); // del
																		// //bug
																		// in
																		// deleting
																		// one
																		// file.
																		// File
																		// not
																		// deleting.
																		// !!!
							myController.setHappendError(false);
						}

					} catch (FileNotFoundException e) {
						sResult.add("Error. File not found. Sorry.");
					} catch (NullPointerException e) {
						sResult.add("Error. Null pointerException. Sorry.");
					} catch (FileAlreadyExistException e) {
						sResult.add("Error. File already exist. Sorry.");
					}
				}
			} else {
				Log.info("User's answer: No.");
				sResult.add(command + " - Operation canceled.");
			}
		}

		return sResult;
	}

	private void showTree(ArrayList<String> aResultOfCommand) {
		DefaultListModel myModel = new DefaultListModel();

		for (int i = 0; i < aResultOfCommand.size(); i++) {
			myModel.addElement(aResultOfCommand.get(i));
		}

		if (lastListFocus.equalsIgnoreCase("left"))
			listLeft.setModel(myModel);

		if (lastListFocus.equalsIgnoreCase("right"))
			listRight.setModel(myModel);
	}

	private void initializeRootButtons(final String wichList) {
		File fDir = new File("");
		File[] arrayRoots = fDir.listRoots();
		JButton[] btnRoot = new JButton[arrayRoots.length];

		for (int i = 0; i < arrayRoots.length; i++) {
			btnRoot[i] = new JButton(arrayRoots[i].getPath());
			final String nameB = arrayRoots[i].getPath();
			btnRoot[i].addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (wichList.equalsIgnoreCase("left")) {
						txtLeft.setText(nameB);
						refreshListBox("left");
					}
					if (wichList.equalsIgnoreCase("right")) {
						txtRight.setText(nameB);
						refreshListBox("right");
					}
				}
			});
			if (wichList.equalsIgnoreCase("left"))
				panelRootLeft.add(btnRoot[i]);
			if (wichList.equalsIgnoreCase("right"))
				panelRootRight.add(btnRoot[i]);
		}
	}

	private static void openXML(String fileNameWithDir) {
		MySAXParser myParser = new MySAXParser(fileNameWithDir);
		ArrayList<String> aResult = myParser.getResult();
		displayResultInSuperWindow(aResult, "XML");
	}

	public class SeparateThread extends Thread {
		private ArrayList<String> myResult = new ArrayList<String>();
		public String myCommand;

		public SeparateThread(String command) {
			myCommand = command;
		}

		public ArrayList<String> getResult() {
			return myResult;
		}

		public void run() {
			tryExecuteCommand(myCommand);
			myResult.add("Operation compleate.");
		}
	}

	private ArrayList<String> tryExecuteCommandSeparateThread(String command) {
		ArrayList<String> result = new ArrayList<String>();
		if (command.startsWith("copy ") || command.startsWith("move ")) {
			SeparateThread myThread = new SeparateThread(command);
			myThread.start();
			result = myThread.getResult();
			if (command.startsWith("copy ")) {
				if (lastListFocus.equalsIgnoreCase("left"))
					refreshListBox("right");
				if (lastListFocus.equalsIgnoreCase("right"))
					refreshListBox("left");
			} else {
				refreshListBox("left");
				refreshListBox("right");
			}
		} else {
			result = tryExecuteCommand(command);
		}
		return result;
	}

	private static void displayResultInSuperWindow(ArrayList<String> result,
			String command) {
		ShowWindow newWindow = new ShowWindow();
		newWindow.setText(result);
		newWindow.setCommand(command);
		newWindow.setWichListRefresh(wichListTree);
		newWindow.show();
	}

	private void actionCopy() {

		Object selectValue = null;
		if (lastListFocus.equalsIgnoreCase("left"))
			selectValue = listLeft.getSelectedValue();
		if (lastListFocus.equalsIgnoreCase("right"))
			selectValue = listRight.getSelectedValue();
		if (selectValue == null)
			JOptionPane.showMessageDialog(null, "Select value, please!",
					"Super Commander", JOptionPane.INFORMATION_MESSAGE);
		{
			String command = "copy " + myController.getWorkDir()
					+ File.separator + (String) selectValue + " ";
			if (lastListFocus.equalsIgnoreCase("left"))
				command += txtRight.getText();
			if (lastListFocus.equalsIgnoreCase("right"))
				command += txtLeft.getText();
			command += File.separator + (String) selectValue;

			tryExecuteCommandSeparateThread(command);

			if (lastListFocus.equalsIgnoreCase("left"))
				refreshListBox("right");
			if (lastListFocus.equalsIgnoreCase("right"))
				refreshListBox("left");
		}
	}

	private void actionMove() {
		Object selectValue = null;
		if (lastListFocus.equalsIgnoreCase("left"))
			selectValue = listLeft.getSelectedValue();
		if (lastListFocus.equalsIgnoreCase("right"))
			selectValue = listRight.getSelectedValue();
		if (selectValue == null)
			JOptionPane.showMessageDialog(null, "Select value, please!",
					"Super Commander", JOptionPane.INFORMATION_MESSAGE);
		else {

			String command = "move " + myController.getWorkDir()
					+ File.separator + (String) selectValue + " ";
			if (lastListFocus.equalsIgnoreCase("left"))
				command += txtRight.getText();
			if (lastListFocus.equalsIgnoreCase("right"))
				command += txtLeft.getText();
			command += File.separator + (String) selectValue;

			tryExecuteCommandSeparateThread(command);

			refreshListBox("right");
			refreshListBox("left");
		}
	}

	private void actionFind() {
		String mask;
		String result = "";
		mask = JOptionPane
				.showInputDialog(null, "Find in: " + myController.getWorkDir()
						+ "\nPlease, input find mask.\nFor example:", "*1*.txt");

		ArrayList<String> aResult = tryExecuteCommandSeparateThread("find "
				+ mask);
		displayResultInSuperWindow(aResult, "find ");
	}

	private void actionTree() {
		wichListTree = lastListFocus;
		ArrayList<String> aResult = tryExecuteCommandSeparateThread("tree "
				+ myController.getWorkDir());
		displayResultInSuperWindow(aResult, "tree ");
	}

	private void actionMkDir() {
		String nameNewDir = JOptionPane.showInputDialog(null,
				"Please, input nameof new directory.", "newDir");
		tryExecuteCommandSeparateThread("mkdir " + nameNewDir);
		refreshListBox(lastListFocus);
	}

	private void actionDel() {
		Object selectValue = null;
		if (lastListFocus.equalsIgnoreCase("left"))
			selectValue = listLeft.getSelectedValue();
		if (lastListFocus.equalsIgnoreCase("right"))
			selectValue = listRight.getSelectedValue();
		if (selectValue == null)
			JOptionPane.showMessageDialog(null, "Select value, please!",
					"Super Commander", JOptionPane.INFORMATION_MESSAGE);
		else {
			tryExecuteCommandSeparateThread("del " + (String) selectValue);
			refreshListBox(lastListFocus);
		}
	}

	private void actionExit() {
		tryExecuteCommandSeparateThread("exit");
	}
}